using AutoMapper;
using B.S.Data.MES.API.Applications.BaseData.Command.CustomerCommand;
using B.S.Domain.基础数据.客户管理;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;

namespace B.S.Data.MES.API.Applications.BaseData.CommandHandlers.CustomerCommandHandlers
{
    public class UpdateCustomerCommandHandlers : IRequestHandler<UpdateCustomerCommand, APIRequst<int>>
    {
        private readonly IBaseRepository<client> customerRepository;
        private readonly IMapper mapper;

        public UpdateCustomerCommandHandlers(IBaseRepository<client> customerRepository, IMapper mapper)
        {
            this.customerRepository = customerRepository;
            this.mapper = mapper;
        }

        public async Task<APIRequst<int>> Handle(UpdateCustomerCommand request, CancellationToken cancellationToken)
        {
            APIRequst<int> response = new APIRequst<int>();
            response.Code = RequstCode.成功;
            response.Msg = RequstCode.成功.ToString();
            var existingCustomer = mapper.Map<client>(request);
            existingCustomer.UpdateTime = DateTime.Now;
            existingCustomer.CreateTime = DateTime.Now;
            await customerRepository.Update(existingCustomer);
            return response;
        }
    }
}